<template>
  <el-container class="home_container">
    <!-- 头部区域 -->
    <el-header>
      <div class="header_left">
        <img src="../assets/heima.png" alt="logo" />
        <span> 这是标题</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>

    <el-container>
      <!-- 侧边栏区域 -->
      <el-aside :width="collapse ? '64px' : '200px'">
        <!-- 侧边栏菜单 -->
        <el-menu
          class="el-menu-vertical-demo"
          background-color="#333844"
          text-color="#fff"
          active-text-color="#2E94FB"
          unique-opened
          :collapse="collapse"
          :collapse-transition="false"
          :default-opened="false"
          router
          :default-active="activePath"
        >
          <!-- 切换折叠展开按钮 -->
          <div class="toggleCollapse" @click="toggleCollapse">|||</div>
          <!-- 一级菜单 -->
          <el-submenu
            :index="subItem.id + ''"
            v-for="subItem in menuList"
            :key="subItem.id"
          >
            <template slot="title">
              <!-- 图标 -->
              <i :class="iconObj[subItem.id]"></i>
              <span>{{ subItem.authName }}</span>
            </template>

            <!-- 二级菜单 -->
            <el-menu-item
              :index="'/' + item.path"
              v-for="item in subItem.children"
              :key="item.id"
              @click="saveNavState('/' + item.path)"
            >
              <!-- 图标 -->
              <i class="el-icon-menu"></i>
              {{ item.authName }}
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 主页面 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  name: 'Home',
  data() {
    return {
      // 菜单列表数据
      menuList: [],
      // icon图标数据
      iconObj: {
        125: 'iconfont icon-user',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao',
      },
      // 当前的折叠状态
      collapse: false,
      // 当前导航二级菜单路径
      activePath: '/user',
    }
  },
  methods: {
    // 退出登录
    logout() {
      // 清除会话  转到登录页面
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    // 获取菜单数据
    async getMenuList() {
      const { data: res } = await this.$http.get('menus')
      // 如果执行有误 弹出信息框
      if (res.meta.status !== 200) return this.$message.error(res.meat.msg)
      this.menuList = res.data
    },
    // 切换状态
    toggleCollapse() {
      this.collapse = !this.collapse
    },
    // 保存当前导航状态路径
    saveNavState(activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    },
  },
  created() {
    // 获取菜单列表
    this.getMenuList()
  },
  mounted() {
    // 加载页面后  在缓存中获取当前导航
    this.activePath = window.sessionStorage.getItem('activePath')
  },
}
</script>

<style lang="less" scoped>
// 主页容器
.home_container {
  height: 100%;
}
.el-header {
  background: #373e40;
  display: flex;
  justify-content: space-between;
  align-items: center;
  color: #fff;
  font-size: 20px;
  padding-left: 0;
  .header_left {
    display: flex;
    align-items: center;

    span {
      margin-left: 15px;
    }
  }
}
// 侧边栏
.el-aside {
  background: #333844;
  .el-menu {
    border-right: 0;
    // 切换按钮
    .toggleCollapse {
      width: 100%;
      background: #454f60;
      color: #fff;
      font-size: 12px;
      text-align: center;
      line-height: 24px;
      letter-spacing: 0.1em;
      cursor: pointer;
    }
  }
}

// 侧边栏菜单
.el-main {
  background: #e6eaec;
}

.iconfont {
  margin-right: 10px;
}
</style>